# Maintainer: Daurnimator <daurnimator@archlinux.org>
# Maintainer: Caleb Maclennan <caleb@alerque.com>
# Contributor: Tom Payne <twpayne@gmail.com>
# Contributor:  <godeater@gmail.com>

pkgname=chezmoi
pkgver=2.41.0
pkgrel=1
pkgdesc="Manage your dotfiles across multiple machines"
arch=('loong64' 'x86_64')
url='https://www.chezmoi.io/'
license=('MIT')
makedepends=('go' 'git')
depends=('glibc')
checkdepends=('zip')
options=('!lto')
source=("${pkgname}-${pkgver}.tar.gz::https://github.com/twpayne/chezmoi/archive/v${pkgver}.tar.gz")
sha512sums=('aa9e85033c2e74af966b0927bf7fea5cb91f56f7cdd9f41d60a6030a05c1ec73a4335b1f5b11e5477a8ce38ec60cbe045357030814d7de5a7d06d1414086731e')

prepare() {
  cd "$pkgname-$pkgver"

  go mod download
}

build() {
  cd "$pkgname-$pkgver"
  export GOPROXY=https://goproxy.cn
  go mod edit -replace=golang.org/x/sys=github.com/golang/sys@v0.0.0-20220622161953-175b2fd9d664
  go mod edit -replace=golang.org/x/net=github.com/golang/net@v0.0.0-20220622184535-263ec571b305
  go mod edit -replace=go.etcd.io/bbolt=go.etcd.io/bbolt@master
  go mod tidy

  export CGO_LDFLAGS="${LDFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPROXY=off
  go build \
    -buildmode=pie \
    -mod=readonly \
    -tags noupgrade \
    -ldflags "-compressdwarf=false \
              -X github.com/twpayne/chezmoi/cmd.DocsDir=/usr/share/doc/$pkgname \
              -X main.version=$pkgver \
              -X main.date=$(date -u +'%Y-%m-%dT%H:%M:%SZ' --date=@${SOURCE_DATE_EPOCH}) \
              -extldflags ${LDFLAGS}" \
    .
}

check() {
  cd "$pkgname-$pkgver"

  go test -v ./...
}

package() {
  cd "$pkgname-$pkgver"

  install -D "$pkgname" "$pkgdir/usr/bin/$pkgname"

  install -Dm644 completions/chezmoi-completion.bash "$pkgdir/usr/share/bash-completion/completions/chezmoi"
  install -Dm644 completions/chezmoi.fish "$pkgdir/usr/share/fish/vendor_completions.d/chezmoi.fish"
  install -Dm644 completions/chezmoi.zsh "$pkgdir/usr/share/zsh/site-functions/_chezmoi"

  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
